﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;


namespace ActionSelectionAttribute.Controllers
{
    public class AcceptsLanguagesAttribute : ActionMethodSelectorAttribute
    {
        public ICollection<string> Languages { get; private set; }

        public AcceptsLanguagesAttribute(params string[] languages)
        {
            Languages = new ReadOnlyCollection<string>(languages);
        }

        public override bool IsValidForRequest(ControllerContext controllerContext,
                                                    MethodInfo methodInfo)
        {
            string lang = controllerContext.HttpContext.Request.UserLanguages[0];
            return Languages.Contains<string>(lang,
                                            StringComparer.OrdinalIgnoreCase);
        }
    }
}
